<template>
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">-->
    <!--      <el-form-item label="用户id关联用户，一对一" prop="userId">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.userId"-->
    <!--          placeholder="请输入用户id关联用户，一对一"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="是否有工作经验(0否，1是)" prop="isWorkExperience">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.isWorkExperience"-->
    <!--          placeholder="请输入是否有工作经验(0否，1是)"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="个人优势信息" prop="advantage">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.advantage"-->
    <!--          placeholder="请输入个人优势信息"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="附件简历地址" prop="attachment">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.attachment"-->
    <!--          placeholder="请输入附件简历地址"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="资格证书, 公共字符串分割" prop="credential">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.credential"-->
    <!--          placeholder="请输入资格证书, 公共字符串分割"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="创建用户" prop="createUser">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.createUser"-->
    <!--          placeholder="请输入创建用户"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="删除标识" prop="isDel">-->
    <!--        <el-input-->
    <!--          v-model="queryParams.isDel"-->
    <!--          placeholder="请输入删除标识"-->
    <!--          clearable-->
    <!--          @keyup.enter="handleQuery"-->
    <!--        />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
    <!--        <el-button icon="Refresh" @click="resetQuery">重置</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['nmzp:resume:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="Edit"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['nmzp:resume:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="Delete"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['nmzp:resume:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="Download"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['nmzp:resume:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resumeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="学历信息" align="center" prop="educations"/>
      <el-table-column label="是否有工作经验" align="center" prop="isWorkExperience">
        <template #default="scope">
          <div>
            <span v-if="scope.row.isWorkExperience === 0">否</span>
            <span v-if="scope.row.isWorkExperience === 1">是</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="工作经验信息" align="center" prop="workExperience">
        <template #default="scope">
          <div v-if="scope.row.isWorkExperience === '1'">{{ scope.row.workExperience }}</div>
        </template>
      </el-table-column>
      <el-table-column label="个人优势信息" align="center" prop="advantage"/>
      <el-table-column label="项目经验" align="center" prop="projectExperience"/>
<!--      <el-table-column label="附件简历地址" align="center" prop="attachment"/>-->
      <el-table-column label="求职状态" align="center" prop="jobSeekingStatus">
        <template #default="scope">
          <div>
            <span v-if="scope.row.jobSeekingStatus === 0">离校-正在找工作</span>
            <span v-if="scope.row.jobSeekingStatus === 1">在校-正在找工作</span>
            <span v-if="scope.row.jobSeekingStatus === 2">在校-看看机会</span>
            <span v-if="scope.row.jobSeekingStatus === 3">在线-暂时不找工作</span>
            <span v-if="scope.row.jobSeekingStatus === 4">离校-我要当牛马</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="资格证书" align="center" prop="credential"/>
      <el-table-column label="专业技能" align="center" prop="professionalSkill"/>
      <el-table-column label="自我评估" align="center" prop="selfEvaluation"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nmzp:resume:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleAttachment(scope.row)"
                     v-hasPermi="['nmzp:resume:attachment']">附件管理
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['nmzp:resume:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改简历管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="resumeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="简历名称" prop="resumeName">
          <el-input v-model="form.resumeName" type="text" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="学历信息" prop="educations">
          <el-input v-model="form.educations" type="text" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="自我总结" prop="advantage">
          <el-input v-model="form.advantage" placeholder="请输入个人优势信息"/>
        </el-form-item>
        <el-form-item label="求职状态" prop="jobSeekingStatus">
          <el-select v-model="form.jobSeekingStatus" placeholder="请选择求职状态">
            <el-option label="离校-正在找工作" value="0"></el-option>
            <el-option label="在校-正在找工作" value="1"></el-option>
            <el-option label="在校-看看机会" value="2"></el-option>
            <el-option label="在线-暂时不找工作" value="3"></el-option>
            <el-option label="离校-我要当牛马" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否有工作经验" prop="isWorkExperience">
          <el-radio-group v-model="form.isWorkExperience">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="工作信息" prop="workExperience" v-if="form.isWorkExperience === '1'">
          <el-input v-model="form.workExperience" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="项目经验" prop="projectExperience">
          <el-input v-model="form.projectExperience" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="资格证书" prop="credential">
          <el-input v-model="form.credential" placeholder="请输入资格证书, 公共字符串分割"/>
        </el-form-item>
        <el-form-item label="专业技能" prop="professionalSkill">
          <el-input v-model="form.professionalSkill" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="自我评估" prop="selfEvaluation">
          <el-input v-model="form.selfEvaluation" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="openAttachment" width="500px" append-to-body>
      <el-form ref="resumeRefAttachment" :model="formAttachment" :rules="rules" label-width="80px">
        <el-form-item>
          <el-button type="primary" @click="downloadAttachment">附件简历查看</el-button>
        </el-form-item>
        <el-form-item label="上传简历" prop="resume">
          <el-upload
              action="#"
              list-type="picture-card"
              :auto-upload="false"
              :file-list="fileList"
              @change="handleFileChange"
          >
            <el-icon>
              <Plus />
            </el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormAttachment">更新附件</el-button>
          <el-button @click="cancelAttachment">取 消</el-button>
        </div>
      </template>
    </el-dialog>



  </div>
</template>

<script setup name="Resume">
import {listResume, getResume, delResume, addResume, updateResume, uploadResumeFile} from "@/api/nmzp/resume";
import { ElMessage } from 'element-plus';
import {getCurrentInstance} from "vue";

const {proxy} = getCurrentInstance();

const resumeList = ref([]);
const open = ref(false);
const openAttachment = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const formAttachment = reactive({
  resume: '',
});




const data = reactive({
  form: {},

  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    educations: null,
    isWorkExperience: null,
    workExperience: null,
    advantage: null,
    projectExperience: null,
    attachment: null,
    jobType: null,
    jobSeekingStatus: null,
    credential: null,
    professionalSkill: null,
    selfEvaluation: null,
    createUser: null,
    isDel: null
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);



/** 查询简历管理列表 */
function getList() {
  loading.value = true;
  listResume(queryParams.value).then(response => {
    resumeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    resumeId: null,
    resumeName: null,
    userId: null,
    educations: null,
    isWorkExperience: null,
    workExperience: null,
    advantage: null,
    projectExperience: null,
    attachment: null,
    jobType: null,
    jobSeekingStatus: null,
    credential: null,
    professionalSkill: null,
    selfEvaluation: null,
    createTime: null,
    createUser: null,
    updateTime: null,
    isDel: null
  };
  proxy.resetForm("resumeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.resumeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加简历管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _resumeId = row.resumeId.toString() || ids.value.toString()
  console.log(_resumeId)
  getResume(_resumeId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改简历管理";
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["resumeRef"].validate(valid => {
    if (valid) {
      if (form.value.resumeId != null) {
        updateResume(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addResume(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _resumeIds = row.resumeId || ids.value;
  proxy.$modal.confirm('是否确认删除简历管理编号为"' + _resumeIds + '"的数据项？').then(function () {
    return delResume(_resumeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('nmzp/resume/export', {
    ...queryParams.value
  }, `resume_${new Date().getTime()}.xlsx`)
}

const  resumeId=ref(null);
const attachmentUrl=ref(null);//简历url
/**
 * 附件管理按钮
 * @param row
 */
function handleAttachment(row) {
  const _resumeId = row.resumeId || ids.value
  resumeId.value=_resumeId;
  attachmentUrl.value=row.attachment;
  getResume(_resumeId).then(response => {
    form.value = response.data;
    openAttachment.value = true;
    title.value = "修改简历管理";
  });
}

const fileList = reactive([]);
const handleFileChange = (file, fileListArray) => {
  fileList.value = fileListArray;
};
function cancelAttachment() {
  openAttachment.value = false;

}

const submitFormAttachment = async () => {

  proxy.$refs.resumeRefAttachment.validate(async (valid) => {
    if (valid) {
      try {
        const formData = new FormData();
        formData.append('resumeId', resumeId.value); // 添加记录的id
        if (fileList.value.length > 0) {
          for (const file of fileList.value) {
            formData.append('file', file.raw);
          }
        }
        await uploadResumeFile(formData);
        ElMessage.success('修改成功');
        location.reload();
      } catch (error) {
        ElMessage.error('操作失败');
      }
    }
  });
};
const downloadAttachment = () => {
  const url = 'http://localhost:8080'+attachmentUrl.value;
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', 'filename'); // 设置下载文件的默认名称
  document.body.appendChild(link);
  link.click();
  document.body.removeChild
}
getList();
</script>
